<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-11
 * Time: 下午12:02
 */
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author hanj
 * 移动端用户接口类(用户类, 店长等身份用)
 */
class regist extends API_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('upload_model','mupload');
        $this->load->model('coltypes_model','mcoltypes');
        $this->load->model('account_info_model','maccountInfo');
        $this->load->model('company_model','mcompany');
        $this->userInfoFields = 'id,phone,nickname';
    }
    protected $rules = array(
        "go" => array(
            array(
                "field" => "uname",
                "label" => "姓名",
                "rules" => "trim|required|min_length[2]|max_length[5]|is_chinese",
            )
        , array(
                "field" => "phone",
                "label" => "手机号码",
                "rules" => "trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_account_check",
            ),
            array(
                "field" => "position",
                "label" => "职位",
                "rules" => "trim|required",
            )
        , array(
                "field" => "company_name",
                "label" => "公司全称",
                "rules" => "trim|required",
            )
        , array(
                "field" => "short",
                "label" => "公司简称",
                "rules" => "trim|required",
            )
        , array(
                "field" => "smscode",
                "label" => "手机验证码",
                "rules" => "trim|required|min_length[6]|max_length[6]|callback_smscode_check",
            )
        , array(
                "field" => "pwd",
                "label" => "登录密码",
                "rules" => "trim|required|min_length[6]|max_length[12]",
            )
        , array(
                "field" => "repwd",
                "label" => "确认密码",
                "rules" => "trim|required|min_length[6]|max_length[12]|matches[pwd]",
            )
        , array(
                "field" => "card",
                "label" => "名片",
                "rules" => "trim|required|callback_photo_check",
            )
        , array(
                "field" => "ctype",
                "label" => "用户类别",
                "rules" => "trim|required|callback_ctype_check",
            )
        ),
        // 注册时用
        "smsCode" => array(
            array(
                'field' => 'phone',
                'label' => '手机号',
                'rules' => 'trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_account_check',
            ),
        ),
    );

    // 用户注册
    public function go() {
        // 返回服务器时间以及预定义参数
        $this->vdata['timeline'] = time();
        $this->vdata['content'] = '';
        $this->vdata['secure'] = 0;
        if($this->data['ctype'] == 3) {
            unset($this->rules['go'][2]);
            unset($this->rules['go'][3]);
            unset($this->rules['go'][4]);
        }
        // 验证
        $this->form_validation->set_rules($this->rules['go']);

        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = validation_errors();
        } else {

            $account_data = array(
                'phone' => $this->data['phone'],
                'ctype' => $this->data['ctype'],
                'uname' => $this->data['uname'],
                'nickname' => $this->data['uname'],
                'pwd' => passwd($this->data['pwd']),
                'position' => isset($this->data['position']) ? $this->data['position'] : '',
                'board' => isset($this->data['board']) ? $this->data['board'] : '',
                'reg_time' => time(),
            );
            $card = $this->mupload->get_one($this->data['card']);
            $info_data = array(
                'card' => $this->data['card'],
                'ctype' => $this->data['ctype'],
                'thumb' => UPLOAD_URL.$card['thumb'],
            );
            if($this->data['ctype'] != 3 ) {
                $company_data = array(
                    'name' => $this->data['company_name'],
                    'short' => $this->data['short'],
                    'initials' => pinyin($this->data['company_name'],'UTF8',true)
                );
                $company = $this->mcompany->get_one($company_data);
                if(!$company) {
                    $company_id = $this->mcompany->create($company_data);
                    $company_uid = 0;
                } else {
                    $company_id = $company['id'];
                    $company_uid = $company['uid'];
                }
                $account_data['company'] = $company_id;
            }

            // 创建数据
            if ($id = $this->macc->create($account_data)) {
                if (!isset($this->data['terminalNo'])) {
                    $this->data['terminalNo'] = 3;
                }
                if($this->data['ctype'] == 1 && !$company_uid) {
                    $this->mcompany->update(array('uid'=>$id),array(
                        'id' => $company_uid
                    ));
                }
                $info_data['uid'] = $id;
                $this->maccountInfo->create($info_data);
                // 返回用户详细数据
                if ($info = $this->mvacc->get_info(array('id' => $id), 'fresh' , $this->data['terminalNo'], $this->userInfoFields)) {

                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = $info;
                } else {
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作失败';
                }
            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作失败';
            }
        }

        // 返回json数据
        $this->_send_json($this->vdata);
    }

    // 发送验证码(创建用户时用)
    public function smsCode() {
        // 返回服务器时间以及预定义参数
        $this->vdata['timeline'] = time();
        $this->vdata['content'] = '';
        $this->vdata['secure'] = 0;

        // 验证
        $this->form_validation->set_rules($this->rules['smsCode']);

        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = validation_errors();
        } else {
            $this->load->model('smscode_model', 'msmscode');
            if ($content = $this->msmscode->sendCode($this->data['phone'], 1)) {
                if ($content['code'] != 200) {
                    $this->vdata['returnCode'] = '0011';
                    $this->vdata['returnInfo'] = $content['msg'];
                    $this->vdata['secure'] = JSON_SECURE;

                } else {
                    // 返回成功
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = $content['msg'];
                }

            } else {
                // 返回成功
                $this->vdata['returnCode'] = '0011';
                $this->vdata['returnInfo'] = '短信发送失败';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = '';
            }
        }

        // 返回json数据
        $this->_send_json($this->vdata);
    }

    //////////////////////////////////////
    ///////////////规则验证////////////////
    //////////////////////////////////////

    // 检验找回密码账户
    public function account_check($account) {
        // 查找用户
        if ($this->info = $this->macc->get_one(array('phone' => $account,'status <>' => 0))) {
            $this->form_validation->set_message('account_check', '该手机号已经被注册！');
            return false;
        } else {
            return true;
        }
    }
    //检查图片
    public function photo_check($photo) {
        // 查找用户
        $photo_check = $this->mupload->get_one($photo);
        if (!$photo_check) {
            $this->form_validation->set_message('photo_check', '上传名片路径有误');
            return false;
        } else {
            return true;
        }
    }
    //检查用户身份
    public function ctype_check($ctype){
        $type = $this->mcoltypes->get_one($ctype);
        if (!$type) {
            $this->form_validation->set_message('ctype_check', '选择用户分类有误');
            return false;
        } else {
            return true;
        }
    }

    public function smscode_check($smscode) {
        $this->load->model('smscode_model', 'msmscode');
        if(ENVIRONMENT != 'development') {
            if (!$this->msmscode->verifySmsCode($this->data['phone'], $smscode, 1)) {
                // 返回失败
                $this->form_validation->set_message('smscode_check', '验证码验证失败,请重新填写');
                return false;
            }
        }

        return true;
    }
}
